####################################
###   Contra Costa and Bay Area  ###
###        Cleaning Data         ###
###         Laura Uribe          ###
###     Created: 06/04/2025      ###
###     Updated: 06/11/2025      ###
####################################

library(readr)
library(tidyverse)
library(dplyr)
library(knitr)
library(texreg)
library(kableExtra)
library(stargazer)
library(car)
library(beepr)
library(ggplot2)
library(tidyr)

# Header -----------------------------------------------------------------------

if(Sys.info()[7] == "laura"){
  setwd("/Users/laura/Dropbox/RA/MIT Evolving Election Administration Landscape/Contra Costa and Bay Area Coalition/")
}

df <- read.csv("2_data/df_clean_botless.csv")

# Analyses ---------------------------------------------------------------------

model_p1 <- lm(ownvote_post ~ poster_treatment_pooledControl + ownvote_pre, data = df, weights = weights)
model_p2 <- lm(bayarea_post ~ poster_treatment_pooledControl + bayarea_pre, data = df, weights = weights)
model_p3 <- lm(california_post ~ poster_treatment_pooledControl + california_pre, data = df, weights = weights)

model_v1 <- lm(ownvote_post ~ video_treatment + ownvote_pre, data = df, weights = weights)
model_v2 <- lm(contracosta_post ~ video_treatment + contracosta_pre, data = df, weights = weights)
model_v3 <- lm(california_post ~ video_treatment + california_pre, data = df, weights = weights)

linearHypothesis(model_v1, "video_treatmentT Video - 4 Times = video_treatmentT Video - 48 Steps")
linearHypothesis(model_v2, "video_treatmentT Video - 4 Times = video_treatmentT Video - 48 Steps")
linearHypothesis(model_v3, "video_treatmentT Video - 4 Times = video_treatmentT Video - 48 Steps")

model_m1 <- lm(web ~ poster_treatment_pooledControl, data = df, weights = weights)
model_m2 <- lm(web ~ video_treatment, data = df, weights = weights)
model_m3 <- lm(petition ~ poster_treatment_pooledControl, data = df, weights = weights)
model_m4 <- lm(petition ~ video_treatment, data = df, weights = weights)
model_m5 <- lm(poll_worker ~ poster_treatment_pooledControl, data = df, weights = weights)
model_m6 <- lm(poll_worker ~ video_treatment, data = df, weights = weights)
model_m7 <- lm(intent_2024 ~ poster_treatment_pooledControl, data = df, weights = weights)
model_m8 <- lm(intent_2024 ~ video_treatment, data = df, weights = weights)

htmlreg(model_p2,
        file = "4_visuals/Collaboration Paper/posters_bayareavote_prepost.html",
        #custom.header = "Trust in Elections (1-4)",
        #custom.model.names = c("Bay Area Vote"),
        custom.coef.names = c("(Intercept)",
                              "Treatment - Ballots",
                              "Treatment - Cell Phone",
                              "Treatment - Iceberg",
                              "Pre-Treatment Trust in Bay Area Vote"),
        caption = "Effect of different poster treatments on post-treatment trust levels",
        caption.above = TRUE)

htmlreg(list(model_p1, model_p2, model_p3),
        file = "4_visuals/Collaboration Paper/posters_prepost.html",
        custom.header = list("Trust in Elections (1-4)" = 1:3),
        custom.model.names = c("Own Vote", "Bay Area Vote", "California Vote"),
        custom.coef.names = c("(Intercept)",
                              "Treatment - Ballots",
                              "Treatment - Cell Phone",
                              "Treatment - Iceberg",
                              "Pre-Treatment Trust in Own Vote",
                              "Pre-Treatment Trust in Bay Area Vote",
                              "Pre-Treatment Trust in California Vote"),
        caption = "Effect of different poster treatments on post-treatment trust levels",
        caption.above = TRUE)

htmlreg(list(model_v1, model_v2, model_v3),
        file = "4_visuals/Collaboration Paper/videos_prepost.html",
        custom.header = list("Trust in Elections (1-4)" = 1:3),
        custom.model.names = c("Own Vote", "Contra Costa Vote", "California Vote"),
        custom.coef.names = c("(Intercept)",
                              "Treatment - 4 Times",
                              "Treatment - 48 Steps",
                              "Pre-Treatment Trust in Own Vote",
                              "Pre-Treatment Trust in Contra Costa Vote",
                              "Pre-Treatment Trust in California Vote"),
        caption = "Effect of different video treatments on post-treatment trust levels",
        caption.above = TRUE)

htmlreg(model_v2,
        file = "4_visuals/Collaboration Paper/videos_contracostavote_prepost.html",
        custom.coef.names = c("(Intercept)",
                              "Treatment - 4 Times",
                              "Treatment - 48 Steps",
                              "Pre-Treatment Trust in Contra Costa Vote"),
        caption = "Effect of different video treatments on post-treatment trust levels",
        caption.above = TRUE)

htmlreg(list(model_m1, model_m2, 
             model_m3, model_m4,
             model_m5, model_m6,
             model_m7, model_m8),
        file = "4_visuals/Collaboration Paper/miscDVs.html",
        custom.header = list("Visit Web Page" = 1:2,
                             "Sign a Petition" = 3:4,
                             "Become a Poll Worker" = 5:6,
                             "2024 Vote Intent" = 7:8),
        custom.coef.names = c("(Intercept)",
                              "Treatment - Ballots",
                              "Treatment - Cell Phone",
                              "Treatment - Iceberg",
                              "Treatment - 4 Times",
                              "Treatment - 48 Steps"),
        caption = "Effect of different poster and video treatments on political behaviors",
        caption.above = TRUE)

# Testing Spillover ------------------------------------------------------------

df_subsetPosterC <- subset(df, poster_treatment_pooledControl=="Control")
df_subsetPosterT <- subset(df, poster_treatment_pooledControl!="Control")

model_v1.sC <- lm(ownvote_post ~ video_treatment + ownvote_pre, data = df_subsetPosterC, weights = weights)
model_v1.sT <- lm(ownvote_post ~ video_treatment + ownvote_pre, data = df_subsetPosterT, weights = weights)

model_v2.sC <- lm(contracosta_post ~ video_treatment + contracosta_pre, data = df_subsetPosterC, weights = weights)
model_v2.sT <- lm(contracosta_post ~ video_treatment + contracosta_pre, data = df_subsetPosterT, weights = weights)

model_v3.sC <- lm(california_post ~ video_treatment + california_pre, data = df_subsetPosterC, weights = weights)
model_v3.sT <- lm(california_post ~ video_treatment + california_pre, data = df_subsetPosterT, weights = weights)


summary(model_v1.sC)
summary(model_v1.sT)

summary(model_v2.sC)
summary(model_v2.sT)

summary(model_v3.sC)
summary(model_v3.sT)

model_names <- c(
  "Own Vote (Poster Control)",
  "Own Vote (Poster Treatment)",
  "Contra Costa Vote (Poster Control)",
  "Contra Costa Vote (Poster Treatment)",
  "California Vote (Poster Control)",
  "California Vote (Poster Treatment)"
)

htmlreg(
  list(
    model_v1.sC, model_v1.sT,
    model_v2.sC, model_v2.sT,
    model_v3.sC, model_v3.sT
  ),
  file = "4_visuals/Collaboration Paper/Spillover_PosterSubsets.html",
  custom.model.names = model_names,
  custom.header = list("Trust in Elections (1-4)" = 1:6),
  custom.coef.names = c("Intercept", "Treatment: 4 Times", "Treatment: 48 Steps", 
                        "Pre-Treatment Trust in Own Vote", "Pre-Treatment Trust in Contra Costa Vote", "Pre-Treatment Trust in California Vote"),
  single.row = TRUE,
  caption = "Spillover effect of different video treatments on post-treatment trust levels",
  caption.above = TRUE
)


df_subsetVideoC <- subset(df, video_treatment=="C Video - Cars")
df_subsetVideoT <- subset(df, video_treatment!="C Video - Cars")

model_p1.sC <- lm(ownvote_post ~ poster_treatment_pooledControl + ownvote_pre, data = df_subsetVideoC, weights = weights)
model_p1.sT <- lm(ownvote_post ~ poster_treatment_pooledControl + ownvote_pre, data = df_subsetVideoT, weights = weights)

model_p2.sC <- lm(bayarea_post ~ poster_treatment_pooledControl + bayarea_pre, data = df_subsetVideoC, weights = weights)
model_p2.sT <- lm(bayarea_post ~ poster_treatment_pooledControl + bayarea_pre, data = df_subsetVideoT, weights = weights)

model_p3.sC <- lm(california_post ~ poster_treatment_pooledControl + california_pre, data = df_subsetVideoC, weights = weights)
model_p3.sT <- lm(california_post ~ poster_treatment_pooledControl + california_pre, data = df_subsetVideoT, weights = weights)

summary(model_p1.sC)
summary(model_p1.sT)

summary(model_p2.sC)
summary(model_p2.sT)

summary(model_p3.sC)
summary(model_p3.sT)

model_names <- c(
  "Own Vote (Video Control)",
  "Own Vote (Video Treatment)",
  "Bay Area Vote (Video Control)",
  "Bay Area Vote (Video Treatment)",
  "California Vote (Video Control)",
  "California Vote (Video Treatment)"
)

htmlreg(
  list(
    model_p1.sC, model_p1.sT,
    model_p2.sC, model_p2.sT,
    model_p3.sC, model_p3.sT
  ),
  file = "4_visuals/Collaboration Paper/Spillover_VideoSubsets.html",
  custom.model.names = model_names,
  custom.header = list("Trust in Elections (1-4)" = 1:6),
  custom.coef.names = c("Intercept", "Treatment: Ballots", "Treatment: Cell Phone", "Treatment: Iceberg", 
                         "Pre-Treatment Trust in Own Vote", "Pre-Treatment Trust in Bay Area Vote", "Pre-Treatment Trust in California Vote"),
  single.row = TRUE,
  caption = "Spillover effect of different poster treatments on post-treatment trust levels",
  caption.above = TRUE
)

